package cn.bin758.h5game.module.game.controller.admin.record;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.bin758.h5game.framework.common.pojo.PageResult;
import cn.bin758.h5game.framework.common.pojo.CommonResult;
import static cn.bin758.h5game.framework.common.pojo.CommonResult.success;

import cn.bin758.h5game.framework.excel.core.util.ExcelUtils;

import cn.bin758.h5game.framework.operatelog.core.annotations.OperateLog;
import static cn.bin758.h5game.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.bin758.h5game.module.game.controller.admin.record.vo.*;
import cn.bin758.h5game.module.game.dal.dataobject.record.RecordDO;
import cn.bin758.h5game.module.game.convert.record.RecordConvert;
import cn.bin758.h5game.module.game.service.record.RecordService;

@Api(tags = "管理后台 - 对局信息")
@RestController
@RequestMapping("/game/record")
@Validated
public class RecordController {

    @Resource
    private RecordService recordService;

    @PostMapping("/create")
    @ApiOperation("创建对局信息")
    @PreAuthorize("@ss.hasPermission('game:record:create')")
    public CommonResult<Long> createRecord(@Valid @RequestBody RecordCreateReqVO createReqVO) {
        return success(recordService.createRecord(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新对局信息")
    @PreAuthorize("@ss.hasPermission('game:record:update')")
    public CommonResult<Boolean> updateRecord(@Valid @RequestBody RecordUpdateReqVO updateReqVO) {
        recordService.updateRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除对局信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('game:record:delete')")
    public CommonResult<Boolean> deleteRecord(@RequestParam("id") Long id) {
        recordService.deleteRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得对局信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('game:record:query')")
    public CommonResult<RecordRespVO> getRecord(@RequestParam("id") Long id) {
        RecordDO record = recordService.getRecord(id);
        return success(RecordConvert.INSTANCE.convert(record));
    }

    @GetMapping("/list")
    @ApiOperation("获得对局信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('game:record:query')")
    public CommonResult<List<RecordRespVO>> getRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<RecordDO> list = recordService.getRecordList(ids);
        return success(RecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得对局信息分页")
    @PreAuthorize("@ss.hasPermission('game:record:query')")
    public CommonResult<PageResult<RecordRespVO>> getRecordPage(@Valid RecordPageReqVO pageVO) {
        PageResult<RecordDO> pageResult = recordService.getRecordPage(pageVO);
        return success(RecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/page2")
    @ApiOperation("获得对局信息分页")
    @PreAuthorize("@ss.hasPermission('game:record:query')")
    public CommonResult<PageResult<RecordRespVO>> getRecordPage2(@Valid RecordPageReqVO pageVO) {
        PageResult<RecordDO> pageResult = recordService.getRecordPage2(pageVO);
        return success(RecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出对局信息 Excel")
    @PreAuthorize("@ss.hasPermission('game:record:export')")
    @OperateLog(type = EXPORT)
    public void exportRecordExcel(@Valid RecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RecordDO> list = recordService.getRecordList(exportReqVO);
        // 导出 Excel
        List<RecordExcelVO> datas = RecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "对局信息.xls", "数据", RecordExcelVO.class, datas);
    }

}
